<form role="form" method="post" action="temp.php" enctype="multipart/form-data">
	<div>
		<input type="file" name="datatxtfile" id="datatxtfile"/>
	</div>
	<input type="submit" class="btn btn-default" value="Upload"/>
	<input type="button" name="a" class="btn btn-default" value="Cancel" onclick="hist()"/>
</form>			
<?PHP
if(isset($_FILES['datatxtfile']))
{
	$allowedExts = array("txt", "csv");
	$extension = end(explode(".", $_FILES["datatxtfile"]["name"]));
	if(in_array($extension, $allowedExts))
	{
		if ($_FILES["datatxtfile"]["error"] > 0)
			echo "Error: " . $_FILES["datatxtfile"]["error"];
	  	else
			uploadTxtData($_FILES["datatxtfile"]["tmp_name"]);
	}
	else
	  	echo "Invalid File";
}
else
{
	// $conn = mysql_connect($cfg["connection"]["host"], $cfg["connection"]["userName"], $cfg["connection"]["password"]) 
		// or die ('Error Cannot Connect to MySQL');
	// @mysql_select_db($cfg["connection"]["database"]) or die( "Unable to select database");
	
	// $query = "drop table product";
	// $result = mysql_query($query);
	// $query = "create table product (name varchar(100), category varchar(50), 
	          // manufacturer varchar(50), barcode int, costprice double, 
	          // currentstock int, minstock int, primary key (barcode));";
	// $result = mysql_query($query);
}							

function uploadTxtData($filename)
{	
	$conn = mysql_connect($host, $db_username, $db_password) 
	        or die ('Error Cannot Connect to MySQL');
	@mysql_select_db('testing') or die('ERROR: mysql_select_db()');

	$file = fopen( $filename, "r" ) or die ('ERROR: fopen()');
	$filesize = filesize( $filename );
	$filetext = fread( $file, $filesize );
	fclose( $file );
	$content = explode("\n", $filetext);
	
	foreach($content as $line)
	{
		list($name, $category, $manuf, $barcode, $costprice, 
		$currentStock, $minStock, $bundleunit) = explode(":",$line);
		$query = "insert into product values ('".$name."',
		         '".$category."','".$manuf."','".$barcode."',
		         '".$costprice."','".$currentStock."',
		         '".$minStock."','".$bundleunit."')";
		$result = mysql_query($query);
	}	

	$query = "Select * From product";
	$result = mysql_query($query);
	echo '<table class="table table-condensed table-hover table-bordered">';
	echo "<thead><th>Product Name</th><th>Category</th><th>Manufacturer</th><th>
	      Barcode</th><th>Cost Price</th><th>Current Stock</th><th>
	      Minimum Stock</th><th>Bundle Unit</th></tr></thead><tbody>";
	
	while($row = mysql_fetch_array($result)) 
	{
		echo "<tr>";
		echo "<td>".$row['name']."</td>";
		echo "<td>".$row['category']."</td>";
		echo "<td>".$row['manufacturer']."</td>";
		echo "<td>".$row['barcode']."</td>";
		echo "<td>".$row['costprice']."</td>";
		echo "<td>".$row['currentstock']."</td>";
		echo "<td>".$row['minstock']."</td>";
		echo "<td>".$row['bundleunit']."</td>";
		echo "</tr>";
	}
	
	echo "</table>";
}	
?>